package com.scada.service.impl;

import java.util.List;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scada.dao.IPermissionRoleDao;
import com.scada.inf.PermissionRoleVisitor;
import com.scada.model.bean.PermissionRole;
import com.scada.model.result.ResponseBean;

@Service("permissionRoleVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class PermissionRoleVisitorImpl implements PermissionRoleVisitor {

	@Autowired
	public IPermissionRoleDao iPermissionRoleDao;
	

	@Override
	public ResponseBean selectPermissionByRoleKey(Integer roleId) {
		ResponseBean rb = new ResponseBean();
		try{
			List<PermissionRole> perList = iPermissionRoleDao.selectByRoleKey(roleId);
			rb.success();
			rb.addObject("perList", perList);
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	@Transactional
	public ResponseBean savePermissionByRoleKey(Integer roleId, List<PermissionRole> perList) {
		
		ResponseBean rb = new ResponseBean();
		try{
			//先进行删除
			Integer size = iPermissionRoleDao.deleteByRoleKey(roleId);
			if(perList != null && perList.size() != 0){
				size = iPermissionRoleDao.insertManyPermission(perList);
				if(size == perList.size()){
					rb.success();
				}else{
					rb.failed();
					throw new RuntimeSqlException("保存失败");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

}
